package com.james.happyfarm.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.james.happyfarm.JDO.Message;
import com.james.happyfarm.JDO.PMF;

public class ShowStatus extends HttpServlet {

	private static final long serialVersionUID = 1032073797039924688L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		resp.setCharacterEncoding("UTF-8");
		try {
			String query = "select from " + Message.class.getName() + " order by date desc limit 20";
			List<Message> msgList = (List<Message>)pm.newQuery(query).execute();
			
			PrintWriter pw = resp.getWriter();
			for (Message msg : msgList) {
				pw.write("[" + msg.getDate() + "] from " + msg.getFrom() + ": " + msg.getMsg() + "<br />");
			}
			pw.flush();
		} finally {
			pm.close();
		}
	}

}
